package com.service.impl;

import com.ReturnDTO.ReturnDTO;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

/**
 * 登录业务层
 */
@Service
public class LoginServiceImpl implements LoginService {

  @Autowired
  private UserMapper userMapper;

  /**
   * 登录判断（500，失败，200，成功）
   * @param user
   * @return
   */
  @Override
  public ReturnDTO userLogin(User user) {


    User formUser = userMapper.selectUserByUserName(user.getUsername());
    if (ObjectUtils.isEmpty(formUser)){
      return ReturnDTO.getReturnDTO(500,"你输入的账号不存在");
    }
    user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
    if (!formUser.getPassword().equals(user.getPassword())){
      return ReturnDTO.getReturnDTO(500,"你輸入的用戶名或者密碼錯誤");
    }
    return ReturnDTO.getReturnDTO(200,"登錄成功",formUser);

  }

}
